#include"link_list.h"

ListNode* createNode(void *val){
    ListNode *newnode=(ListNode*)malloc(sizeof(ListNode));
    newnode->val=val;
    newnode->next=NULL;
    return newnode;
}

void appendNode(ListNode **head,void* val){
    ListNode *newnode=createNode(val);
    if(*head==NULL){
        *head=newnode;
        return;
    }
    ListNode* current=*head;
    while(current->next!=NULL){
        current=current->next;
    }
    current->next=newnode;
}

void deleteNode(ListNode** head,void* target){
    if(*head==NULL) return;

    if((*head)->val==target){
        ListNode *temp=*head;
        *head=(*head)->next;
        free(temp);
        return;
    }

    ListNode* current=*head;
    while(current->next!=NULL&&current->next->val!=target){
        current=current->next;
    }

    if(current->next!=NULL){
        ListNode* temp=current->next;
        current->next=current->next->next;
        free(temp);
    }
}

void freeList(ListNode *head){
    ListNode* current=head;
    while(current!=NULL){
        ListNode *temp=current;
        current=current->next;
        free(temp);
    }
}
